Programming Questions for Practice
Given an integer n, calculate the first n numbers in the Fibonacci sequence given starting elements of (0,1). Return an array of n integers, including the given (0, 1) in the sequence. Use the function fibonacci().
#include <stdio.h>
#include <stdlib.h>
/*
Given an integer n, calculate the first n numbers in the
Fibonacci sequence given starting elements of (0,1).
Return an array of n integers, including the given (0, 1)
in the sequence. Use the function fibonacci().
*/
int* fibonacci(int length) {
int *list = (int *)calloc(length, sizeof(int));
int previous_1 = 0, previous_2 = 1, count = 2, next;
list[0] = previous_1; list[1] = previous_2;
while(count <= length) {
next = previous_1 + previous_2;
list[count] = next;
previous_1 = previous_2;
previous_2 = next;
count++;
}
return list;
}
int main()
{
int length, count;
int *result;
printf("Enter the length: ");
scanf("%d", &length);
result = fibonacci(length);
for(count = 0; count < length; count++)
printf("%d\n", result[count]);
return 0;
}
Code not available...
Code not available...